package cn.iocoder.yudao.module.picturebook.controller.admin.workmember;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.workmember.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.workmember.WorkMemberDO;
import cn.iocoder.yudao.module.picturebook.service.workmember.WorkMemberService;

@Tag(name = "管理后台 - 作业与成员关系表	")
@RestController
@RequestMapping("/picturebook/work-member")
@Validated
public class WorkMemberController {

    @Resource
    private WorkMemberService workMemberService;

    @PostMapping("/create")
    @Operation(summary = "创建作业与成员关系表	")
    @PreAuthorize("@ss.hasPermission('picturebook:work-member:create')")
    public CommonResult<Long> createWorkMember(@Valid @RequestBody WorkMemberSaveReqVO createReqVO) {
        return success(workMemberService.createWorkMember(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新作业与成员关系表	")
    @PreAuthorize("@ss.hasPermission('picturebook:work-member:update')")
    public CommonResult<Boolean> updateWorkMember(@Valid @RequestBody WorkMemberSaveReqVO updateReqVO) {
        workMemberService.updateWorkMember(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作业与成员关系表	")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:work-member:delete')")
    public CommonResult<Boolean> deleteWorkMember(@RequestParam("id") Long id) {
        workMemberService.deleteWorkMember(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除作业与成员关系表	")
                @PreAuthorize("@ss.hasPermission('picturebook:work-member:delete')")
    public CommonResult<Boolean> deleteWorkMemberList(@RequestParam("ids") List<Long> ids) {
        workMemberService.deleteWorkMemberListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得作业与成员关系表	")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:work-member:query')")
    public CommonResult<WorkMemberRespVO> getWorkMember(@RequestParam("id") Long id) {
        WorkMemberDO workMember = workMemberService.getWorkMember(id);
        return success(BeanUtils.toBean(workMember, WorkMemberRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得作业与成员关系表	分页")
    @PreAuthorize("@ss.hasPermission('picturebook:work-member:query')")
    public CommonResult<PageResult<WorkMemberRespVO>> getWorkMemberPage(@Valid WorkMemberPageReqVO pageReqVO) {
        PageResult<WorkMemberDO> pageResult = workMemberService.getWorkMemberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkMemberRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作业与成员关系表	 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:work-member:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkMemberExcel(@Valid WorkMemberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkMemberDO> list = workMemberService.getWorkMemberPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "作业与成员关系表	.xls", "数据", WorkMemberRespVO.class,
                        BeanUtils.toBean(list, WorkMemberRespVO.class));
    }

}